<html>
<head>
<title>Main.java</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<style type="text/css">
.s0 { color: #cc7832;}
.s1 { color: #a9b7c6;}
.s2 { color: #808080;}
.s3 { color: #6a8759;}
.s4 { color: #6897bb;}
</style>
</head>
<body bgcolor="#2b2b2b">
<table CELLSPACING=0 CELLPADDING=5 COLS=1 WIDTH="100%" BGCOLOR="#606060" >
<tr><td><center>
<font face="Arial, Helvetica" color="#000000">
Main.java</font>
</center></td></tr></table>
<pre><span class="s0">package </span><span class="s1">com.company</span><span class="s0">;</span>

<span class="s0">import </span><span class="s1">java.util.ArrayList</span><span class="s0">;</span>

<span class="s0">public class </span><span class="s1">Main {</span>
    <span class="s0">static </span><span class="s1">ArrayList&lt;StudentScore&gt; studentScores = </span><span class="s0">new </span><span class="s1">ArrayList&lt;&gt;()</span><span class="s0">;</span>

    <span class="s0">public static void </span><span class="s1">main(String[] args) {</span>
        <span class="s2">// 创建一些学生成绩对象并添加到列表中</span>
        <span class="s1">studentScores.add(</span><span class="s0">new </span><span class="s1">StudentScore(</span><span class="s3">&quot;001&quot;</span><span class="s0">, </span><span class="s3">&quot;张三&quot;</span><span class="s0">, </span><span class="s3">&quot;女&quot;</span><span class="s0">, </span><span class="s4">85</span><span class="s0">, </span><span class="s4">90</span><span class="s0">, </span><span class="s4">92</span><span class="s1">))</span><span class="s0">;</span>
        <span class="s1">studentScores.add(</span><span class="s0">new </span><span class="s1">StudentScore(</span><span class="s3">&quot;002&quot;</span><span class="s0">, </span><span class="s3">&quot;李四&quot;</span><span class="s0">, </span><span class="s3">&quot;男&quot;</span><span class="s0">, </span><span class="s4">90</span><span class="s0">, </span><span class="s4">88</span><span class="s0">, </span><span class="s4">86</span><span class="s1">))</span><span class="s0">;</span>
        <span class="s1">studentScores.add(</span><span class="s0">new </span><span class="s1">StudentScore(</span><span class="s3">&quot;003&quot;</span><span class="s0">, </span><span class="s3">&quot;王五&quot;</span><span class="s0">, </span><span class="s3">&quot;女&quot;</span><span class="s0">, </span><span class="s4">88</span><span class="s0">, </span><span class="s4">92</span><span class="s0">, </span><span class="s4">90</span><span class="s1">))</span><span class="s0">;</span>

        <span class="s2">// 测试计算语文的最高分</span>
        <span class="s1">System.out.println(</span><span class="s3">&quot;语文的最高分: &quot; </span><span class="s1">+ getMaxLanguageScore())</span><span class="s0">;</span>

        <span class="s2">// 测试计算数学的平均分</span>
        <span class="s1">System.out.println(</span><span class="s3">&quot;数学的平均分: &quot; </span><span class="s1">+ getAverageMathScore())</span><span class="s0">;</span>

        <span class="s2">// 测试统计男生和女生的人数</span>
        <span class="s1">countGenders()</span><span class="s0">;</span>

        <span class="s2">// 测试根据英语成绩进行排序输出【从高到低】</span>
        <span class="s1">sortByEnglishScore()</span><span class="s0">;</span>
    <span class="s1">}</span>

    <span class="s2">// 遍历输出所有数据</span>
    <span class="s0">public static void </span><span class="s1">showAll() {</span>
        <span class="s0">for </span><span class="s1">(StudentScore studentScore : studentScores) {</span>
            <span class="s1">System.out.println(studentScore)</span><span class="s0">;</span>
        <span class="s1">}</span>
    <span class="s1">}</span>

    <span class="s2">// 计算语文的最高分</span>
    <span class="s0">public static int </span><span class="s1">getMaxLanguageScore() {</span>
        <span class="s0">if </span><span class="s1">(studentScores == </span><span class="s0">null </span><span class="s1">|| studentScores.isEmpty()) {</span>
            <span class="s0">return </span><span class="s4">0</span><span class="s0">;</span>
        <span class="s1">}</span>
        <span class="s0">int </span><span class="s1">maxScore = studentScores.get(</span><span class="s4">0</span><span class="s1">).getLanguage()</span><span class="s0">;</span>
        <span class="s0">for </span><span class="s1">(StudentScore studentScore : studentScores) {</span>
            <span class="s0">int </span><span class="s1">currentScore = studentScore.getLanguage()</span><span class="s0">;</span>
            <span class="s0">if </span><span class="s1">(currentScore &gt; maxScore) {</span>
                <span class="s1">maxScore = currentScore</span><span class="s0">;</span>
            <span class="s1">}</span>
        <span class="s1">}</span>
        <span class="s0">return </span><span class="s1">maxScore</span><span class="s0">;</span>
    <span class="s1">}</span>

    <span class="s2">// 计算数学的平均分</span>
    <span class="s0">public static double </span><span class="s1">getAverageMathScore() {</span>
        <span class="s0">if </span><span class="s1">(studentScores == </span><span class="s0">null </span><span class="s1">|| studentScores.isEmpty()) {</span>
            <span class="s0">return </span><span class="s4">0</span><span class="s0">;</span>
        <span class="s1">}</span>
        <span class="s0">int </span><span class="s1">sum = </span><span class="s4">0</span><span class="s0">;</span>
        <span class="s0">for </span><span class="s1">(StudentScore studentScore : studentScores) {</span>
            <span class="s1">sum += studentScore.getMath()</span><span class="s0">;</span>
        <span class="s1">}</span>
        <span class="s0">return </span><span class="s1">(</span><span class="s0">double</span><span class="s1">) sum / studentScores.size()</span><span class="s0">;</span>
    <span class="s1">}</span>

    <span class="s2">// 统计男生和女生的人数</span>
    <span class="s0">public static void </span><span class="s1">countGenders() {</span>
        <span class="s0">int </span><span class="s1">maleCount = </span><span class="s4">0</span><span class="s0">;</span>
        <span class="s0">int </span><span class="s1">femaleCount = </span><span class="s4">0</span><span class="s0">;</span>
        <span class="s0">for </span><span class="s1">(StudentScore studentScore : studentScores) {</span>
            <span class="s0">if </span><span class="s1">(</span><span class="s3">&quot;男&quot;</span><span class="s1">.equals(studentScore.getSex())) {</span>
                <span class="s1">maleCount++</span><span class="s0">;</span>
            <span class="s1">} </span><span class="s0">else if </span><span class="s1">(</span><span class="s3">&quot;女&quot;</span><span class="s1">.equals(studentScore.getSex())) {</span>
                <span class="s1">femaleCount++</span><span class="s0">;</span>
            <span class="s1">}</span>
        <span class="s1">}</span>
        <span class="s1">System.out.println(</span><span class="s3">&quot;男生人数: &quot; </span><span class="s1">+ maleCount)</span><span class="s0">;</span>
        <span class="s1">System.out.println(</span><span class="s3">&quot;女生人数: &quot; </span><span class="s1">+ femaleCount)</span><span class="s0">;</span>
    <span class="s1">}</span>

    <span class="s2">// 根据英语成绩进行排序输出【从高到低】</span>
    <span class="s0">public static void </span><span class="s1">sortByEnglishScore() {</span>
        <span class="s1">studentScores.sort((s1</span><span class="s0">, </span><span class="s1">s2) -&gt; s2.getEnglish() - s1.getEnglish())</span><span class="s0">;</span>
        <span class="s0">for </span><span class="s1">(StudentScore studentScore : studentScores) {</span>
            <span class="s1">System.out.println(studentScore)</span><span class="s0">;</span>
        <span class="s1">}</span>
    <span class="s1">}</span>
<span class="s1">}</span>

<span class="s0">class </span><span class="s1">StudentScore {</span>
    <span class="s2">// 学号属性</span>
    <span class="s0">private </span><span class="s1">String studentID</span><span class="s0">;</span>
    <span class="s2">// 姓名属性</span>
    <span class="s0">private </span><span class="s1">String name</span><span class="s0">;</span>
    <span class="s2">// 性别属性</span>
    <span class="s0">private </span><span class="s1">String sex</span><span class="s0">;</span>
    <span class="s2">// 语文属性</span>
    <span class="s0">private int </span><span class="s1">language</span><span class="s0">;</span>
    <span class="s2">// 数学属性</span>
    <span class="s0">private int </span><span class="s1">math</span><span class="s0">;</span>
    <span class="s2">// 英语属性</span>
    <span class="s0">private int </span><span class="s1">english</span><span class="s0">;</span>

    <span class="s1">@Override</span>
    <span class="s0">public </span><span class="s1">String toString() {</span>
        <span class="s0">return </span><span class="s3">&quot;StudentScore{&quot; </span><span class="s1">+</span>
                <span class="s3">&quot;studentID='&quot; </span><span class="s1">+ studentID + </span><span class="s3">'</span><span class="s0">\'</span><span class="s3">' </span><span class="s1">+</span>
                <span class="s3">&quot;, name='&quot; </span><span class="s1">+ name + </span><span class="s3">'</span><span class="s0">\'</span><span class="s3">' </span><span class="s1">+</span>
                <span class="s3">&quot;, sex='&quot; </span><span class="s1">+ sex + </span><span class="s3">'</span><span class="s0">\'</span><span class="s3">' </span><span class="s1">+</span>
                <span class="s3">&quot;, language=&quot; </span><span class="s1">+ language +</span>
                <span class="s3">&quot;, math=&quot; </span><span class="s1">+ math +</span>
                <span class="s3">&quot;, english=&quot; </span><span class="s1">+ english +</span>
                <span class="s3">'}'</span><span class="s0">;</span>
    <span class="s1">}</span>

    <span class="s0">public </span><span class="s1">StudentScore() {</span>
    <span class="s1">}</span>

    <span class="s0">public </span><span class="s1">StudentScore(String studentID</span><span class="s0">, </span><span class="s1">String name</span><span class="s0">, </span><span class="s1">String sex</span><span class="s0">, int </span><span class="s1">language</span><span class="s0">, int </span><span class="s1">math</span><span class="s0">, int </span><span class="s1">english) {</span>
        <span class="s0">this</span><span class="s1">.studentID = studentID</span><span class="s0">;</span>
        <span class="s0">this</span><span class="s1">.name = name</span><span class="s0">;</span>
        <span class="s0">this</span><span class="s1">.sex = sex</span><span class="s0">;</span>
        <span class="s0">this</span><span class="s1">.language = language</span><span class="s0">;</span>
        <span class="s0">this</span><span class="s1">.math = math</span><span class="s0">;</span>
        <span class="s0">this</span><span class="s1">.english = english</span><span class="s0">;</span>
    <span class="s1">}</span>

    <span class="s0">public </span><span class="s1">String getStudentID() {</span>
        <span class="s0">return </span><span class="s1">studentID</span><span class="s0">;</span>
    <span class="s1">}</span>

    <span class="s0">public void </span><span class="s1">setStudentID(String studentID) {</span>
        <span class="s0">this</span><span class="s1">.studentID = studentID</span><span class="s0">;</span>
    <span class="s1">}</span>

    <span class="s0">public </span><span class="s1">String getName() {</span>
        <span class="s0">return </span><span class="s1">name</span><span class="s0">;</span>
    <span class="s1">}</span>

    <span class="s0">public void </span><span class="s1">setName(String name) {</span>
        <span class="s0">this</span><span class="s1">.name = name</span><span class="s0">;</span>
    <span class="s1">}</span>

    <span class="s0">public </span><span class="s1">String getSex() {</span>
        <span class="s0">return </span><span class="s1">sex</span><span class="s0">;</span>
    <span class="s1">}</span>

    <span class="s0">public void </span><span class="s1">setSex(String sex) {</span>
        <span class="s0">this</span><span class="s1">.sex = sex</span><span class="s0">;</span>
    <span class="s1">}</span>

    <span class="s0">public int </span><span class="s1">getLanguage() {</span>
        <span class="s0">return </span><span class="s1">language</span><span class="s0">;</span>
    <span class="s1">}</span>

    <span class="s0">public void </span><span class="s1">setLanguage(</span><span class="s0">int </span><span class="s1">language) {</span>
        <span class="s0">this</span><span class="s1">.language = language</span><span class="s0">;</span>
    <span class="s1">}</span>

    <span class="s0">public int </span><span class="s1">getMath() {</span>
        <span class="s0">return </span><span class="s1">math</span><span class="s0">;</span>
    <span class="s1">}</span>

    <span class="s0">public void </span><span class="s1">setMath(</span><span class="s0">int </span><span class="s1">math) {</span>
        <span class="s0">this</span><span class="s1">.math = math</span><span class="s0">;</span>
    <span class="s1">}</span>

    <span class="s0">public int </span><span class="s1">getEnglish() {</span>
        <span class="s0">return </span><span class="s1">english</span><span class="s0">;</span>
    <span class="s1">}</span>

    <span class="s0">public void </span><span class="s1">setEnglish(</span><span class="s0">int </span><span class="s1">english) {</span>
        <span class="s0">this</span><span class="s1">.english = english</span><span class="s0">;</span>
    <span class="s1">}</span>

<span class="s1">}</span></pre>
</body>
</html>